package com.ms.education.ucenter.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ms.education.R;
import com.ms.education.ucenter.entity.Teacher;
import com.ms.education.ucenter.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 课程的教师 前端控制器
 * </p>
 *
 * @author ms
 * @since 2023-01-12
 */
@RestController
@RequestMapping("/ucenter/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @PostMapping("pageTeacher/{current}/{limit}")
    public R pageTeacher(@RequestBody Teacher teacher,
                         @PathVariable("current") long current,
                         @PathVariable("limit") long limit) {
        return teacherService.pageTeacher(teacher, current, limit);
    }

    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody Teacher teacher) {
        teacherService.updateById(teacher);
        return R.ok();
    }

    @PostMapping("deleteTeacherByIds")
    public R deleteTeacherByIds(@RequestBody Integer[] ids) {
        // TODO 删除教师关联的信息
        teacherService.removeBatchByIds(Arrays.asList(ids));
        return R.ok();
    }

    @GetMapping("getTeacherById/{id}")
    public R getTeacherById(@PathVariable("id") Integer id) {
        Teacher teacher = teacherService.getById(id);
        return R.ok().data("teacher", teacher);
    }

    @PutMapping("saveTeacher")
    public R saveTeacher(Teacher teacher) {
        teacherService.save(teacher);
        return R.ok();
    }

    @GetMapping("getTeacherByName/{name}")
    public R getTeacherByName(@PathVariable("name") String name) {
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        wrapper.like("name", name);
        List<Teacher> teachers = teacherService.list(wrapper);
        return R.ok().data("teachers", teachers);
    }

    @PostMapping("getTeacherByIds")
    public List<Teacher> getTeacherByIds(@RequestBody List<Integer> ids) {
        return teacherService.listByIds(ids);
    }


}
